<template>
  <view>
    <!-- <video :src="videoURL" autoplay loop muted class="video" /> -->
	{{videoURL}}
    <view class="data-json">
      <view class="data-json-item">
        <text>选择视频数据</text>
        <textarea v-model="videoJSONString" placeholder="请求响应数据"></textarea>
      </view>
      <view class="data-json-item">
        <text>分享结果数据</text>
        <textarea v-model="resultJSONString" placeholder="请求响应数据"></textarea>
      </view>
    </view>
    <view class="btns">
      <button @click="selectVideo">选择视频</button>
      <button @click="shareVideo" :disabled="!videoJSON" type="primary">分享视频</button>
    </view>
    <view class="btns">
      <button @click="uniSelectVideo">UNI-APP选择视频</button>
      <button @click="uniShareVideo" :disabled="!videoJSON" type="primary">临时路径分享视频</button>
    </view>
  </view>
</template>

<script>
const DYID = "awl52rl664pxcflk";
const plugin = uni.requireNativePlugin("Vnidev-DyPlugin-Core");

export default {
  data() {
    return {
      videoJSON: null,
      resultJSON: null,
      videoURL: "",
    };
  },

  computed: {
    videoJSONString() {
      return this.videoJSON ? JSON.stringify(this.videoJSON, null, 4) : "";
    },
    resultJSONString() {
      return this.resultJSON ? JSON.stringify(this.resultJSON, null, 4) : "";
    },
  },

  methods: {
    // 选择视频
    selectVideo() {
      plugin.getMediaInfoAndPath(
        {
          type: "video", // 选择类型，图片(image)/视频(video)
          max: 1, // 最多选择多少个，默认5个
          min: 1, //至少选择多少个，默认1个
          withmd5: false, // 选择之后是否同时返回Md5值,默认false
          withcover: true, //选择完之后是否返回封面缩略图，默认false
          withcoverwidth: 600, // 缩略图宽度，默认600px宽，原始尺寸,
        },
        (res) => {
          this.videoJSON = res["selected"];
          this.videoURL = res["selected"][0].path;
        }
      );
    },

    // uni选择视频
    uniSelectVideo() {
      uni.chooseVideo({
        sourceType: ["album"],
        compressed: false,
        success: (res) => {
          this.videoJSON = res;
          this.videoURL = res.tempFilePath;
        },
      });
    },

    // 分享视频
    shareVideo() {
      const mediaIds = this.videoJSON.map((item) => item.identifier);
      plugin.shareMediaViaPaths(
        {
          appid: DYID,
          sharetype: "publish",
          mediaIds: mediaIds,
          sharetype: "video",
          mpInfo: null,
          debug: true,
        },
        (res) => {
          this.resultJSON = res;
        }
      );
    },

    // 分享视频
    uniShareVideo() {
      const paths = [this.videoURL];//[plus.io.convertLocalFileSystemURL(this.videoURL)];
      plugin.shareMediaViaUniDownloadPaths(
        {
          appid: DYID,
          startpage: "publish",
          mediaPaths: paths,
          sharetype: "video",
          mpInfo: null,
          hashtag: "",
          debug: true,
        },
        (res) => {
          this.resultJSON = res;
        }
      );
    },
  },
};
</script>

<style lang="scss">
.video {
  width: 100%;
  height: 300px;
}

.data-json-item {
  padding: 5px;
  text {
    color: blueviolet;
    font-size: 13px;
    padding-left: 5px;
    font-weight: 600;
  }
  textarea {
    background-color: #fff;
    border: 1px solid #ddd;
    padding: 10px;
    box-sizing: border-box;
    width: 100%;
  }
}
.btns {
  display: flex;

  button {
    flex: 1;
    margin: 5px;
  }
}
</style>
